package com.briup.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.briup.bean.Slideshow;
import com.briup.exception.ServiceException;
import com.briup.service.SlideshowService;
import com.briup.mapper.SlideshowMapper;
import com.briup.util.ResultCode;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
* @author 梦晓
* @description 针对表【cms_slideshow】的数据库操作Service实现
* @createDate 2024-11-18 14:40:06
*/
@Service
public class SlideshowServiceImpl extends ServiceImpl<SlideshowMapper, Slideshow>
    implements SlideshowService{
    @Resource
    SlideshowMapper slideshowMapper;

    @Override
    public List<Slideshow> queryAllEnable() {
        LambdaQueryWrapper<Slideshow> qw = new LambdaQueryWrapper<>();
        qw.eq(Slideshow::getStatus, "启用");
        qw.orderByDesc(Slideshow::getUploadTime);
        List<Slideshow> list = slideshowMapper.selectList(qw);
        if(list == null || list.isEmpty())
            throw new ServiceException(ResultCode.DATA_NONE);
        return list;
    }

    @Override
    public IPage<Slideshow> query(Integer pageNum, Integer pageSize, String status, String desc) {
        IPage<Slideshow> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Slideshow> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StringUtils.hasText(status),Slideshow::getStatus,status)
                .like(StringUtils.hasText(desc),Slideshow::getDescription,desc).orderByDesc(Slideshow::getUploadTime);
        slideshowMapper.selectPage(page,lqw);
        if (page.getTotal() == 0)
            throw new ServiceException(ResultCode.DATA_NONE);
        return page;
    }

    @Override
    public Slideshow queryById(Integer id) {
        return slideshowMapper.selectById(id);
    }

    @Override
    public void saveORUpdate(Slideshow slideshow) {
        Integer id = slideshow.getId();
        //1.判断轮播图url是否唯一
        String url = slideshow.getUrl();
        //url唯一标识
        boolean flag = false;
        if (url != null) {
        //判断是否是原来的轮播图url
            if(id != null) {
                Slideshow oldSlideshow =
                        slideshowMapper.selectById(id);
                if(oldSlideshow != null &&
                        url.equals(oldSlideshow.getUrl())) {
                    flag = true;
                }
            }
            //判断url是否唯一
            if(!flag) {
                LambdaQueryWrapper<Slideshow> qw = new
                        LambdaQueryWrapper<>();
                qw.eq(Slideshow::getUrl, url);
                Slideshow s = slideshowMapper.selectOne(qw);
                if (s != null)
                    throw new ServiceException(ResultCode.SLIDESHOW_URL_EXISTED);
                // 重置图片url更新时间
                slideshow.setUploadTime(LocalDateTime.now());
            }
        }
        if (id == null) {
            //2.新增操作
            if (slideshow.getStatus() == null)
                slideshow.setStatus("启用");
            slideshowMapper.insert(slideshow);
        } else {
        //3.更新操作
            //3.1 判断当前轮播图是否有效
            Slideshow s = slideshowMapper.selectById(id);
            if (s == null)
                throw new ServiceException(ResultCode.SLIDESHOW_NOT_EXISTED);
            //3.2 更新操作
            slideshowMapper.updateById(slideshow);
        }
    }

    @Override
    public void deleteByBatch(List<Integer> ids) {
        if (ids==null||ids.isEmpty()){
            throw new ServiceException(ResultCode.PARAM_IS_BLANK);
        }
        LambdaQueryWrapper<Slideshow> lqw = new LambdaQueryWrapper<>();
        lqw.in(Slideshow::getId,ids);
        Integer integer = slideshowMapper.selectCount(lqw);
        if (integer==0){
            throw new ServiceException(ResultCode.SLIDESHOW_NOT_EXISTED);
        }
        slideshowMapper.deleteBatchIds(ids);
    }

}




